package cn.edu.sgu.www.mhxysy.function.computer.score.impl;

import cn.edu.sgu.www.mhxysy.entity.qiling.Qiling;
import cn.edu.sgu.www.mhxysy.entity.qiling.QilingCategory;
import cn.edu.sgu.www.mhxysy.entity.qiling.QilingTaozhuang;
import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.function.computer.score.ScoreComputer;
import cn.edu.sgu.www.mhxysy.mapper.equipment.EquipmentTypeMapper;
import cn.edu.sgu.www.mhxysy.mapper.qiling.QilingAttributeMapper;
import cn.edu.sgu.www.mhxysy.mapper.qiling.QilingCategoryMapper;
import cn.edu.sgu.www.mhxysy.mapper.qiling.QilingMapper;
import cn.edu.sgu.www.mhxysy.mapper.qiling.QilingTaozhuangMapper;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import cn.edu.sgu.www.mhxysy.util.CollectionUtils;
import cn.edu.sgu.www.mhxysy.vo.except.AttributeVO;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 器灵评分计算器
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Component
public class QilingScoreComputer implements ScoreComputer {

    private final QilingMapper qilingMapper;
    private final EquipmentTypeMapper equipmentTypeMapper;
    private final QilingCategoryMapper qilingCategoryMapper;
    private final QilingAttributeMapper qilingAttributeMapper;
    private final QilingTaozhuangMapper qilingTaozhuangMapper;

    public QilingScoreComputer(
            QilingMapper qilingMapper,
            EquipmentTypeMapper equipmentTypeMapper,
            QilingCategoryMapper qilingCategoryMapper,
            QilingAttributeMapper qilingAttributeMapper,
            QilingTaozhuangMapper qilingTaozhuangMapper) {
        this.qilingMapper = qilingMapper;
        this.equipmentTypeMapper = equipmentTypeMapper;
        this.qilingCategoryMapper = qilingCategoryMapper;
        this.qilingAttributeMapper = qilingAttributeMapper;
        this.qilingTaozhuangMapper = qilingTaozhuangMapper;
    }

    /**
     * 计算器灵评分
     * @param qilingId 器灵ID
     * @return Integer 器灵的总评分
     */
    @Override
    public Integer apply(String qilingId) {
        Qiling qiling = qilingMapper.selectById(qilingId);

        if (qiling == null) {
            throw new GlobalException(ResponseCode.CONFLICT, "操作失败，器灵不存在！");
        }

        /*
         * 器灵总评分 = 器灵属性评分 + 器灵套装评分
         * 1、器灵属性评分：计算方式和装备属性评分一样
         * 2、器灵套装评分：2/4/6件套分别增加50、100、150评分（套装件数 × 25）
         */
        AtomicInteger score = new AtomicInteger();

        // 器灵套装评分
        Integer taozhuangId = qiling.getTaozhuangId();

        if (taozhuangId > 0) {
            QilingTaozhuang qilingTaozhuang = qilingTaozhuangMapper.selectById(taozhuangId);

            /*
             * 套装的评分 = 套装件数 × 25
             * 二件套：50
             * 四件套：100
             * 六件套：150
             */
            score.addAndGet(qilingTaozhuang.getCount() * 25);
        }

        // 查询器灵属性
        List<AttributeVO> list = qilingAttributeMapper.selectAttributes(qilingId);

        // 器灵可能没有属性，所以需要先判断
        if(CollectionUtils.isNotEmpty(list)) {
            // 查询器灵类型信息
            QilingCategory qilingCategory = qilingCategoryMapper.selectById(qiling.getCategoryId());
            // 得到顶级装备类型
            Integer type = equipmentTypeMapper.selectTopType(qilingCategory.getEquipmentId());

            // 得到器灵的属性评分
            int computeScore = EquipmentScoreComputer.computeScore(type, list);

            score.addAndGet(computeScore);
        }

        return score.get();
    }

}